use rand::Rng;
use std::cmp::Ordering;
use std::io;

fn main() {
    header();
}

fn header() {
    println!("----------------------------------------@@ 菜谱 @@----------------------------------------");
    println!("@@------------------------------------1: 加法口诀表--------------------------------------@@");
    println!("------------------------------------------------------------------------------------------");
    println!("@@------------------------------------2: 加减法练习场------------------------------------@@");
    println!("------------------------------------------------------------------------------------------");
    println!("@@------------------------------------3: 猜数游戏----------------------------------------@@");
    println!("------------------------------------------------------------------------------------------");
    println!("@@------------------------------------0: 退出菜谱----------------------------------------@@");
    println!("------------------------------------------------------------------------------------------");

    let mut menu = String::new();
    io::stdin().read_line(&mut menu).expect("无法读取行");
    let menu: u32 = match menu.trim().parse() {
        Ok(num) => num,
        Err(_) => 2,
    };

    match menu {
        0 => println!(""),
        1 => formula_table(),
        2 => play(),
        3 => bet(),
        _ => play(),
    }
}

fn bet() {
    println!("猜数游戏!!!");

    let secret_number = rand::thread_rng().gen_range(1, 101);

    // println!("神秘数字是:{}", secret_number);

    let res = loop {
        println!("------------------------------------------------------------------------------------------");
        println!("@@|                                  猜一个1-100的数字                                  |@@");
        println!("------------------------------------------------------------------------------------------");

        let mut guess = String::new();

        io::stdin().read_line(&mut guess).expect("无法读取行");

        let guess: u32 = match guess.trim().parse() {
            Ok(num) => num,
            Err(_) => continue,
        };

        match guess.cmp(&secret_number) {
            Ordering::Less => {
                println!("------------------------------------------------------------------------------------------");
                println!("@@|                                        太小了                                       |@@");
                println!("------------------------------------------------------------------------------------------");
            }
            Ordering::Greater => {
                println!("------------------------------------------------------------------------------------------");
                println!("@@|                                        太大了                                       |@@");
                println!("------------------------------------------------------------------------------------------");
            }
            Ordering::Equal => {
                println!("你赢了!!!");
                println!("@@---------------------按下提示所提供的序号，请你选择接下来任务吧------------------------@@");
                println!(
                    "@@------------------------------------1：继续猜大小--------------------------------------@@"
                );
                println!(
                    "@@------------------------------------2：结束猜大小--------------------------------------@@"
                );

                let mut choose = String::new();

                io::stdin().read_line(&mut choose).expect("无法读取行");
                let choose: u32 = match choose.trim().parse() {
                    Ok(num) => num,
                    Err(_) => 2,
                };

                if choose == 2 {
                    break true;
                }
            }
        }
    };

    if res {
        header();
    }
}
fn formula_table() {
    let res = loop {
        println!("------------------------------------------------------------------------------------------");
        println!("@@|                             小朋友请输入一个数字试试看吧                           |@@");
        println!("------------------------------------------------------------------------------------------");
        let mut guess = String::new();
        io::stdin().read_line(&mut guess).expect("无法读取行");
        let guess: u32 = match guess.trim().parse() {
            Ok(num) => num,
            Err(_) => 10,
        };
        const WIDTH: u32 = 8;
        for elem in 1..guess + 1 {
            let repeated = "-".repeat((elem * WIDTH) as usize);
            println!("{}", repeated);
            for em in 1..elem + 1 {
                print!(
                    "|{0:<width$}+{1:<width$}={2:<width$}|",
                    em,
                    elem,
                    elem + em,
                    width = 2
                );
            }
            println!("");
            print!("{}", repeated);
            println!("");
        }
        println!("@@---------------------按下提示所提供的序号，请你选择接下来任务吧------------------------@@");
        println!("@@-----------------------------------1：继续加法口诀-------------------------------------@@");
        println!("@@-----------------------------------2：结束加法口诀-------------------------------------@@");

        let mut choose = String::new();

        io::stdin().read_line(&mut choose).expect("无法读取行");
        let choose: u32 = match choose.trim().parse() {
            Ok(num) => num,
            Err(_) => 2,
        };

        if choose == 2 {
            break true;
        }
    };

    if res {
        header();
    }
}
fn play() {
    let res = loop {
        println!("------------------------------------------------------------------------------------------");
        println!("@@--------------------(*^_^*)欢迎来到10以内加减法游戏练习场(*^_^*)----------------------@@");
        println!("------------------------------------------------------------------------------------------");
        println!("@@-----------------------------你可以在练习场里肆意玩耍---------------------------------@@");
        println!("------------------------------------------------------------------------------------------");
        println!("@@--------------------------------------共10题------------------------------------------@@");
        println!("------------------------------------------------------------------------------------------");
        println!("@@-------------------------此次练习任重而道远，希望你可以战胜它-------------------------@@");
        println!("------------------------------------------------------------------------------------------");
        println!("@@----------------------~~~~~~~~~~踏上你的练习场之旅吧~~~~~~~~~~~~----------------------@@");
        println!("------------------------------------------------------------------------------------------");
        for elem in 1..11 {
            let left_number = rand::thread_rng().gen_range(1, 11);
            let right_number = rand::thread_rng().gen_range(1, 11);
            match left_number.cmp(&right_number) {
                Ordering::Less => {
                    if right_number % 2 == 0 {
                        println!("------------------------------------------------------------------------------------------");
                        println!("@@|--第{0:<width$}题--|                           {1:<width$} + {2:<width$} =                                  |@@", elem, right_number, left_number,width = 2);
                        println!("------------------------------------------------------------------------------------------");
                        contest(right_number + left_number)
                    } else {
                        println!("------------------------------------------------------------------------------------------");
                        println!("@@|--第{0:<width$}题--|                           {1:<width$} - {2:<width$} =                                  |@@", elem, right_number, left_number,width = 2);
                        println!("------------------------------------------------------------------------------------------");
                        contest(right_number - left_number)
                    }
                }
                Ordering::Greater => {
                    if left_number % 2 == 0 {
                        println!("------------------------------------------------------------------------------------------");
                        println!("@@|--第{0:<width$}题--|                           {1:<width$} + {2:<width$} =                                  |@@", elem, left_number, right_number,width = 2);
                        println!("------------------------------------------------------------------------------------------");
                        contest(left_number + right_number)
                    } else {
                        println!("------------------------------------------------------------------------------------------");
                        println!("@@|--第{0:<width$}题--|                           {1:<width$} - {2:<width$} =                                  |@@", elem, left_number, right_number,width = 2);
                        println!("------------------------------------------------------------------------------------------");
                        contest(left_number - right_number)
                    }
                }
                Ordering::Equal => {
                    println!("------------------------------------------------------------------------------------------");
                    println!("@@|--第{0:<width$}题--|                           {1:<width$} + {2:<width$} =                                  |@@", elem, left_number, right_number,width = 2);
                    println!("------------------------------------------------------------------------------------------");
                    contest(left_number + right_number)
                }
            }
        }
        println!("------------------------------------------------------------------------------------------");
        println!(
            "@@---~~~~~~~~~~ 一场惊险又刺激的练习结束啦,想想你在这次练习中学习到了什么吧 ~~~~~~~~~~---@@"
        );

        println!("------------------------------------------------------------------------------------------");
        println!("@@---------------------按下提示所提供的序号，请你选择接下来任务吧------------------------@@");
        println!("@@---------------------------------1：继续练习场-----------------------------------------@@");
        println!("@@---------------------------------2：结束练习场-----------------------------------------@@");
        let mut choose = String::new();

        io::stdin().read_line(&mut choose).expect("无法读取行");
        let choose: u32 = match choose.trim().parse() {
            Ok(num) => num,
            Err(_) => 2,
        };

        if choose == 2 {
            break true;
        }
    };

    if res {
        header();
    }
}
fn contest(result: u32) {
    loop {
        println!("@@|                               告诉我你的最终答案吧                              |@@");
        println!("------------------------------------------------------------------------------------------");

        let mut guess = String::new();

        io::stdin().read_line(&mut guess).expect("无法读取行");

        let guess: u32 = match guess.trim().parse() {
            Ok(num) => num,
            Err(_) => {
                println!("@@|                                     回答错误                                    |@@");
                println!("------------------------------------------------------------------------------------------");
                continue;
            }
        };

        match guess.cmp(&result) {
            Ordering::Less => {
                println!("------------------------------------------------------------------------------------------");
                println!("@@|                                     回答错误                                    |@@");
                println!("------------------------------------------------------------------------------------------");
                continue;
            }
            Ordering::Greater => {
                println!("------------------------------------------------------------------------------------------");
                println!("@@|                                     回答错误                                    |@@");
                println!("------------------------------------------------------------------------------------------");
                continue;
            }
            Ordering::Equal => {
                println!("------------------------------------------------------------------------------------------");
                println!("@@|                                     回答正确                                    |@@");
                println!("------------------------------------------------------------------------------------------");
                break;
            }
        }
    }
}
